#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
Python 3.x
Flask 2.1.3
Flask-Limiter 2.6.3

Official Manual Address : https://flask-limiter.readthedocs.io/en/stable/index.html

配置Storage
"""
import redis
from flask import Flask
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address

app = Flask(__name__)

# Memcached
limiter = Limiter(
    app,
    key_func=get_remote_address,
    storage_uri="memcached://localhost:11211",
    storage_options={}
)

# Redis
limiter = Limiter(
    app, key_func=get_remote_address,
    storage_uri="redis://localhost:6379",
    storage_options={"connect_timeout": 30},
    strategy="fixed-window",  # or "moving-window"
)

# Redis (reused connection pool)
pool = redis.connection.BlockingConnectionPool.from_url("redis://.....")
limiter = Limiter(
    app, key_func=get_remote_address,
    storage_uri="redis://",
    storage_options={"connection_pool": pool},
    strategy="fixed-window",  # or "moving-window"
)

# Redis Cluster
limiter = Limiter(
    app,
    key_func=get_remote_address,
    storage_uri="redis+cluster://localhost:7000,localhost:7001,localhost:7002",
    storage_options={"connect_timeout": 30},
    strategy="fixed-window",  # or "moving-window"
)

# MongoDB
limiter = Limiter(
    app, key_func=get_remote_address,
    storage_uri="mongodb://localhost:27017",
    strategy="fixed-window",  # or "moving-window"
)
